/********************************************************************************
* File Name:	mainwindow.cpp
* Description:	第3章 实例15：ComboBox、LineEdit和TextEdit的实例
* Reference book:《Linux环境下Qt4图形界面与MySQL编程》，机械工业出版社.2012.1
* E_mail: openlinux2011@gmail.com
*
********************************************************************************/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_btnEqual_clicked()
{
    if(ui->leX->text().isEmpty())
    {
        QMessageBox::warning(NULL, "warning", "Please input X ", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
    }
    if(ui->leY->text().isEmpty())
    {
        QMessageBox::warning(NULL, "warning", "Please input Y ", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
    }

    int x,y;
    str += ui->leX->text()+ui->cbxRule->currentText()+ui->leY->text()+" = ";
    x=ui->leX->text().toInt();
    y=ui->leY->text().toInt();

    if(ui->cbxRule->currentText()=="+")
        str +=QString::number(x+y)+"\n";
    else if(ui->cbxRule->currentText()=="-")
        str +=QString::number(x-y)+"\n";
    else if(ui->cbxRule->currentText()=="*")
        str +=QString::number(x*y)+"\n";
    else if(ui->cbxRule->currentText()=="/")
    {
        if(y!=0)
            str +=QString::number(x/y)+"\n";
        else
        {
            QMessageBox::warning(NULL, "error", "y=0 ", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
            return;
        }

    }


    ui->textEditResult->setText(str);

}
